.text
.align 8
.section .rodata
.LC0:
    .string "Usage: %s filename\n"
.LC1:
    .string "r"
.LC2:
    .string "I could not open the file \"%s\"\n"
.LC3:
    .string ".red"
.LC4:
    .string "w"
.LC5:
    .string "Cannot create output file.\n"
.LC6:
    .string "Error in closing files\n"

.text
.globl main
.type main, @function
main:
    addi sp, sp, -96
    sd ra, 88(sp)
    sd s0, 80(sp)
    addi s0, sp, 80

    // 分配局部变量
    // in: -8(s0)
    // out: -16(s0)
    // ch: -20(s0)
    // name: -60(s0)
    // count: -64(s0)
    // argc: -68(s0)
    // argv: -76(s0)
    sd a1, -76(s0)
    sw a0, -68(s0)

    // 测试一下打印参数
    /**
    ld a0, 0(a1)
    call printf
    */

    sw zero, -64(s0) /* count */
    lw a5, -68(s0) /* argc */
    li a6, 2
    bge a5, a6, .CONTINUE
.EXIT_ARGS_ERROR:
    lui a5, %hi(stderr)
    addi a5, a5, %lo(stderr)
    ld a0, 0(a5)
    lui a5, %hi(.LC0)
    addi a1, a5, %lo(.LC0)
    ld a5, -76(s0) /* argv */
    ld a2, 0(a5)
    call fprintf
    // EXIT_FAILURE
    li a0, 1
    call exit
.CONTINUE:
    ld a5, -76(s0) /* argv */
    ld a0, 8(a5)
    lui a5, %hi(.LC1)
    addi a1, a5, %lo(.LC1)
    call fopen
    sd a0, -8(s0) /* in */
    bne a0, zero, .CONTINUE1
.EXIT_OPEN_READ_FILE_FAILED:
    lui a5, %hi(stderr)
    addi a5, a5, %lo(stderr)
    ld a0, 0(a5)
    lui a5, %hi(.LC2)
    addi a1, a5, %lo(.LC2)
    ld a5, -76(s0) /* argv */
    ld a2, 8(a5)
    call fprintf
    li a0, 3
    call exit
.CONTINUE1:
    addi a0, s0, -60 /* offset of name */
    ld a5, -76(s0) /* argv */
    ld a1, 8(a5)
    li a2, 35
    call strncpy
    addi a5, s0, -60 /* offset of name */
    addi a5, a5, 35
    sb zero, 0(a5)

    addi a0, s0, -60 /* offset of name */
    lui a5, %hi(.LC3)
    addi a1, a5, %lo(.LC3)
    call strcat

    addi a0, s0, -60 /* offset of name */
    lui a5, %hi(.LC4)
    addi a1, a5, %lo(.LC4)
    call fopen
    sd a0, -16(s0) /* out */
    bne a0, zero, .CONTINUE2
.EXIT_OPEN_WRITE_FILE_FAILED:
    lui a5, %hi(stderr)
    addi a5, a5, %lo(stderr)
    sd a0, 0(a5)
    lui a5, %hi(.LC5)
    addi a1, a5, %lo(.LC5)
    call fprintf
    li a0, 3
    call exit
.CONTINUE2:
    j .WHILE_34_COMPARE
.WHILE_34_ENTER:
    lw a5, -64(s0) /* count */
    addiw a6, a5, 1
    sw a6, -64(s0) /* count */
    li a6, 3
    remw a5, a5, a6
    sext.w a5, a5
    bne a5, zero, .WHILE_34_INCR
    lw a0, -20(s0) /* ch */
    ld a1, -16(s0) /* out */
    call putc
.WHILE_34_INCR:
    // Nothing to do
.WHILE_34_COMPARE:
    ld a0, -8(s0) /* in */
    call getc
    sw a0, -20(s0) /* ch */
    sext.w a5, a0
    li a6, -1
    bne a5, a6, .WHILE_34_ENTER

.IF_38:
    ld a0, -8(s0) /* in */
    call fclose
    bne a0, zero, .FILE_CLOSE_ERR
    ld a0, -16(s0) /* out */
    call fclose
    bne a0, zero, .FILE_CLOSE_ERR
    j .EXIT_SUCCESS
.FILE_CLOSE_ERR:
    lui a5, %hi(stderr)
    addi a5, a5, %lo(stderr)
    sd a0, 0(a5)
    lui a5, %hi(.LC6)
    addi a1, a5, %lo(.LC6)
    call fprintf
.EXIT_SUCCESS:
    mv a0, zero
    ld ra, 88(sp)
    ld s0, 80(sp)
    addi sp, sp, 96
    jr ra